package cn.yuan.basic.service.impl;


import cn.yuan.basic.mapper.BaseMapper;
import cn.yuan.basic.query.BaseQuery;
import cn.yuan.basic.service.IBaseService;
import cn.yuan.basic.util.PageList;
import cn.yuan.pet.domain.Pet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public abstract class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    public void remove(Long id) {
        baseMapper.remove(id);
    }

    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    @Override
    public PageList<T> queryForPage(BaseQuery baseQuery) {
        /*1.查询总数量*/
        Integer count = baseMapper.queryCount();
        /*2.判断数量<=0 就没必要查询所有了*/
        if (count <= 0) {
            return new PageList<T>();
        }
        /*3.查询分页数据*/
        List<T> list = baseMapper.queryData(baseQuery);
        PageList<T> pageList = new PageList<T>(count, list);
        return pageList;
    }

}